'''
Created on Jul 14, 2009

@author: user
'''
import unittest
import cliques.utils
from cliques.mdp.mdpBasedSearch import mdpBasedSearch
import cliques.mdp.mdpLookahead
import cliques.mdp.limitedStatesMdp
import cliques.algorithmRunner
import cliques.experiment_runner
import graph
import cProfile
import pstats
class unittest_lookahead(unittest.TestCase):


    def setUp(self):
        pass


    def tearDown(self):
        pass


    def mtestHasCliqueFailure(self):
        GRAPH_NAME = "BugGraphHasClique.graph"
        exp_graph = cliques.utils.import_graph(GRAPH_NAME)
        algorithms = (mdpBasedSearch(cliques.mdp.heuristicMdp.heuristicMdp,"heuristicMdp"),\
            mdpBasedSearch(cliques.mdp.mdpLookahead.mdpLookahead,"lookaheadMdp-0",0),
            mdpBasedSearch(cliques.mdp.mdpLookahead.mdpLookahead,"lookaheadMdp-1",1))
        k=4
        start_node = u'5'
        runner = cliques.algorithmRunner.algorithmRunner()
        
        for algorithm in algorithms:
            algorithm.setup(exp_graph,k,start_node)
            runner.run(algorithm)
    
    def testKnownDegreeBetter(self):
        exp_graph = graph.graph()
        nodes = 10
        edges = 20
        exp_graph.generate(nodes,edges)
        GRAPH_NAME = "testKnownDegreeBetter.graph"
        # To enable recreating the graph
        cliques.utils.export_graph(exp_graph,GRAPH_NAME)
        exp_graph = cliques.utils.import_graph(GRAPH_NAME)
        k=4
        start_node = exp_graph.nodes()[0]
        runner = cliques.algorithmRunner.algorithmRunner()
        knownDegree = cliques.knownDegreeSearch.knownDegreeSearch()
        knownDegree.setup(exp_graph, k, start_node)
        lookahead1 = mdpBasedSearch(cliques.mdp.mdpLookahead.mdpLookahead, "LH1", 1)
        lookahead1.setup(exp_graph, k, start_node)
        runner = cliques.algorithmRunner.algorithmRunner()
        runner.run(knownDegree)
        runner.run(lookahead1)
        assert knownDegree.iteration>=lookahead1.iteration
    
    def testWhyKnownDegreeBetter(self):
        GRAPH_NAME= "testWhyKnownDegreeBetter.graph"
        exp_graph = cliques.utils.import_graph(GRAPH_NAME)
        k=4
        start_node = exp_graph.nodes()[0]
        runner = cliques.algorithmRunner.algorithmRunner()
        knownDegree = cliques.knownDegreeSearch.knownDegreeSearch()
        knownDegree.setup(exp_graph, k, start_node)
        lookahead1 = mdpBasedSearch(cliques.mdp.mdpLookahead.mdpLookahead, "LH1", 1)
        lookahead1.setup(exp_graph, k, start_node)
        runner = cliques.algorithmRunner.algorithmRunner()
        runner.run(knownDegree)
        runner.run(lookahead1)
        assert knownDegree.iteration>=lookahead1.iteration

    
if __name__ == "__main__":
    import sys;sys.argv = ['', 'Test.testName']
    unittest.main()
#    
#cProfile.run("unittest.main()","mdp-test2.profile")    
#profile_stats = pstats.Stats("mdp-test2.profile")
#profile_stats.strip_dirs()
#profile_stats.sort_stats('time')
#profile_stats.print_stats(15)
#    
    
    
    